package ha.web.client.servlet.login;

import ha.web.server.exceptions.LoginException;
import ha.web.server.exceptions.RegisterException;
import ha.web.server.manager.GameMangerServerSide;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Register")
public class Register extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5731412614024141304L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Register() {
		super();
	}

	private void LoginRejected (HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		//check input
		Map<String,String[]> requestParm = request.getParameterMap();
		if(requestParm.size()!=4) {
			LoginRejected(request, response);
			return;
		}
		String userName,userPass,reUserPass,nickName;
		Boolean admin;
		if (!request.getParameterMap().containsKey("user") ||
				!request.getParameterMap().containsKey("pass") ||
				!request.getParameterMap().containsKey("re_pass") || 
				!request.getParameterMap().containsKey("nick_name") 
				) {
			LoginRejected(request, response);
			return;
		}
		userName = request.getParameter("user");
		userPass = request.getParameter("pass");
		reUserPass = request.getParameter("re_pass");
		nickName = request.getParameter("nick_name");
		request.getParameter("admin");
		if (request.getParameterMap().containsKey("admin"))
		{
		admin = true;
		}else{
			admin = false;
		}
		try {
			GameMangerServerSide.getInstance().registerNewUser(userName, userPass, reUserPass, nickName, admin);
		} catch (LoginException | RegisterException e) {
			PrintWriter out = response.getWriter();
			out.print("<h3 style=\"color:#CC0000\">"+e.getMessage()+"</h3>");
			LoginRejected(request, response);
			return;
		}
		ServletContext context = getServletContext();
		RequestDispatcher dispatcher = context.getRequestDispatcher("/Login");
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
		out.print("<HTML><HEAD><TITLE>login page</TITLE>");
		out.print("<META http-equiv=Content-Type content=\"text/html\">");
		out.print("<link rel=\"stylesheet\" type=\"text/css\" href=\"gameDesign.css\" />");
		out.print("</HEAD>");
		out.print("<BODY>");
		out.print("<P>");
		out.print("<!-- send to game window and check there-->");
		out.print("<DIV align=\"center\">");
		out.print("<h2>Welcome</h2>");
		out.print("Please register:");
		out.print("<form action=\"register\" method=\"post\">");
		out.print("Name: <input type=\"text\" name=\"user\"><br>");
		out.print("Nick Name: <input type=\"text\" name=\"nick_name\"><br>");
		out.print("Password: <input type=\"password\" name=\"pass\"><br>");
		out.print("Repeat_Password: <input type=\"password\" name=\"re_pass\"><br>");
		out.print("<input type=\"checkbox\" name=\"admin\" value=\"True\">Administrator<br>");
		out.print("<input type=\"submit\" class=\"button\" value=\"Register\">");
		out.print("<br><button type=\"button\" class=\"button\" onclick=registerRules()>game rules</button>");
		out.print("<script src=\"utils.js\" type=\"text/javascript\"></script>");
		out.print("</form>");
		out.print("</DIV>");
		out.print("<P></P>");
		out.print("</BODY></HTML>");
	}


}
